package com.ruoyi.web.controller.Information;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.Money.domain.Money;
import com.ruoyi.Money.service.IMoneyService;
import com.ruoyi.Order.service.IOrderService;
import com.ruoyi.School.domain.School;
import com.ruoyi.School.service.ISchoolService;
import com.ruoyi.User.service.IUserService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysDept;
import com.ruoyi.system.service.ISysDeptService;

/**
 * 金额流向 信息操作处理
 *
 * @author ruoyi
 * @date 2019-08-09
 */
@Controller
@RequestMapping("/Information")
public class InformationController extends BaseController {
	private String prefix1 = "Information/orderInformation";

	private String prefix2 = "Information/moneyInformation";

	private String prefix3 = "Information/userInformation";

	@Autowired
	private IOrderService orderService;

	@Autowired
	private IUserService userService;

	@Autowired
	private ISchoolService schoolService;

	@GetMapping("/orderInformation")
	public String orderInformation(ModelMap mmap) {
		School example = new School();
		List<School> schoolList = schoolService.selectSchoolList(example);
		mmap.put("schoolList", schoolList);
		return prefix1 + "/Information";
	}

	@GetMapping("/orderInformation/search")
	@ResponseBody
	public Map<String, Object> orderSearch(String year, String month,
			String school, int flag) throws ParseException {
		List<Map<String, Object>> a = new ArrayList<Map<String, Object>>();
		List<String> dateX = new ArrayList<String>();
		List<Integer> monthY = new ArrayList<Integer>();
		if (!(StringUtils.isEmpty(year)) && !(StringUtils.isEmpty(month))) {
			String time = year + "-" + month;
			int schoolId;
			if (StringUtils.isEmpty(school)) {
				schoolId = 0;
			} else {
				schoolId = Integer.parseInt(school);
			}
			List<Map<String, Object>> result = orderService
					.selectCountForInformationMonth(schoolId, time, flag);
			// 退款
			// List<Map<String,Object>>
			// result2=orderService.selectCountForInformationMonth(schoolId,time,1);
			for (Map<String, Object> map : result) {
				dateX.add(map.get("date").toString());
				monthY.add(Integer.parseInt(map.get("count").toString()));
			}
			Map<String, Object> allMonth = new HashMap<String, Object>();
			allMonth.put("dateX", dateX);
			allMonth.put("monthY", monthY);
			return allMonth;
		} else if (!(StringUtils.isEmpty(year)) && (StringUtils.isEmpty(month))) {
			int schoolId;
			if (StringUtils.isEmpty(school)) {
				schoolId = 0;
			} else {
				schoolId = Integer.parseInt(school);
			}
			List<Map<String, Object>> result = orderService
					.selectCountForInformationYear(schoolId, year, flag);
			if (result.size() > 0) {
				Map<String, Object> data = result.get(0);
				dateX = getYearMonth();
				monthY.add(Integer.parseInt(data.get("JAN").toString()));
				monthY.add(Integer.parseInt(data.get("FEB").toString()));
				monthY.add(Integer.parseInt(data.get("MAR").toString()));
				monthY.add(Integer.parseInt(data.get("APR").toString()));
				monthY.add(Integer.parseInt(data.get("MAY").toString()));
				monthY.add(Integer.parseInt(data.get("JUN").toString()));
				monthY.add(Integer.parseInt(data.get("JULY").toString()));
				monthY.add(Integer.parseInt(data.get("AUG").toString()));
				monthY.add(Integer.parseInt(data.get("SEPT").toString()));
				monthY.add(Integer.parseInt(data.get("OCT").toString()));
				monthY.add(Integer.parseInt(data.get("NOV").toString()));
				monthY.add(Integer.parseInt(data.get("DEC").toString()));
				Map<String, Object> allYear = new HashMap<String, Object>();
				allYear.put("dateX", dateX);
				allYear.put("monthY", monthY);
				return allYear;
			} else {
				return null;
			}
		} else {
			Map<String, Object> error=new HashMap<String, Object>();
			return error;
		}

		/*
		 * List<Map<String,Object>> a =new ArrayList<Map<String,Object>>();
		 * Map<String,Object> test1=new HashMap<String, Object>();
		 * test1.put("test1", 1); Map<String,Object> test2=new HashMap<String,
		 * Object>(); test2.put("test2", 1); a.add(test1); a.add(test2);
		 * JSONArray result=JSONArray.fromObject(a);
		 */

	}

	@GetMapping("/moneyInformation")
	public String moneyInformation(ModelMap mmap) {
		School example = new School();
		List<School> schoolList = schoolService.selectSchoolList(example);
		mmap.put("schoolList", schoolList);
		return prefix2 + "/Information";
	}

	@GetMapping("/moneyInformation/search")
	@ResponseBody
	public Map<String, Object> moneySearch(String year, String month,
			String school) throws ParseException {
		List<Map<String, Object>> a = new ArrayList<Map<String, Object>>();
		List<String> dateX = new ArrayList<String>();
		List<Double> monthY = new ArrayList<Double>();
		if (!(StringUtils.isEmpty(year)) && !(StringUtils.isEmpty(month))) {
			String time = year + "-" + month;
			int schoolId;
			if (StringUtils.isEmpty(school)) {
				schoolId = 0;
			} else {
				schoolId = Integer.parseInt(school);
			}
			List<Map<String, Object>> result = orderService
					.selectMoneyForInformationMonth(schoolId, time);
			// 退款
			// List<Map<String,Object>>
			// result2=orderService.selectCountForInformationMonth(schoolId,time,1);
			for (Map<String, Object> map : result) {
				dateX.add(map.get("date").toString());
				monthY.add(Double.parseDouble(map.get("sum").toString()));
			}
			Map<String, Object> allMonth = new HashMap<String, Object>();
			allMonth.put("dateX", dateX);
			allMonth.put("monthY", monthY);
			return allMonth;
		} else if (!(StringUtils.isEmpty(year)) && (StringUtils.isEmpty(month))) {
			int schoolId;
			if (StringUtils.isEmpty(school)) {
				schoolId = 0;
			} else {
				schoolId = Integer.parseInt(school);
			}
			List<Map<String, Object>> result = orderService
					.selectMoneyForInformationYear(schoolId, year);
			if (result.size() > 0) {
				Map<String, Object> data = result.get(0);
				dateX = getYearMonth();
				monthY.add(Double.parseDouble(data.get("JAN").toString()));
				monthY.add(Double.parseDouble(data.get("FEB").toString()));
				monthY.add(Double.parseDouble(data.get("MAR").toString()));
				monthY.add(Double.parseDouble(data.get("APR").toString()));
				monthY.add(Double.parseDouble(data.get("MAY").toString()));
				monthY.add(Double.parseDouble(data.get("JUN").toString()));
				monthY.add(Double.parseDouble(data.get("JULY").toString()));
				monthY.add(Double.parseDouble(data.get("AUG").toString()));
				monthY.add(Double.parseDouble(data.get("SEPT").toString()));
				monthY.add(Double.parseDouble(data.get("OCT").toString()));
				monthY.add(Double.parseDouble(data.get("NOV").toString()));
				monthY.add(Double.parseDouble(data.get("DEC").toString()));
				Map<String, Object> allYear = new HashMap<String, Object>();
				allYear.put("dateX", dateX);
				allYear.put("monthY", monthY);
				return allYear;
			} else {
				return null;
			}
		} else {
			Map<String, Object> error=new HashMap<String, Object>();
			return error;
		}

		/*
		 * List<Map<String,Object>> a =new ArrayList<Map<String,Object>>();
		 * Map<String,Object> test1=new HashMap<String, Object>();
		 * test1.put("test1", 1); Map<String,Object> test2=new HashMap<String,
		 * Object>(); test2.put("test2", 1); a.add(test1); a.add(test2);
		 * JSONArray result=JSONArray.fromObject(a);
		 */

	}

	@GetMapping("/userInformation")
	public String userInformation() {
		return prefix3 + "/Information";
	}

	@GetMapping("/userInformation/search")
	@ResponseBody
	public Map<String, Object> userSearch(String year, String month,int flag) throws ParseException {
		List<Map<String, Object>> a = new ArrayList<Map<String, Object>>();
		List<String> dateX = new ArrayList<String>();
		List<Integer> monthY = new ArrayList<Integer>();
		if (!(StringUtils.isEmpty(year)) && !(StringUtils.isEmpty(month))) {
			String time = year + "-" + month;

			List<Map<String, Object>> result = userService
					.selectUserCountForInformationMonth(flag, time);
			// 退款
			// List<Map<String,Object>>
			// result2=orderService.selectCountForInformationMonth(schoolId,time,1);
			for (Map<String, Object> map : result) {
				dateX.add(map.get("date").toString());
				monthY.add(Integer.parseInt(map.get("count").toString()));
			}
			Map<String, Object> allMonth = new HashMap<String, Object>();
			allMonth.put("dateX", dateX);
			allMonth.put("monthY", monthY);
			return allMonth;
		} else if (!(StringUtils.isEmpty(year)) && (StringUtils.isEmpty(month))) {

			List<Map<String, Object>> result = userService
					.selectUserCountForInformationYear(flag, year);
			if (result.size() > 0) {
				Map<String, Object> data = result.get(0);
				dateX = getYearMonth();
				monthY.add(Integer.parseInt(data.get("JAN").toString()));
				monthY.add(Integer.parseInt(data.get("FEB").toString()));
				monthY.add(Integer.parseInt(data.get("MAR").toString()));
				monthY.add(Integer.parseInt(data.get("APR").toString()));
				monthY.add(Integer.parseInt(data.get("MAY").toString()));
				monthY.add(Integer.parseInt(data.get("JUN").toString()));
				monthY.add(Integer.parseInt(data.get("JULY").toString()));
				monthY.add(Integer.parseInt(data.get("AUG").toString()));
				monthY.add(Integer.parseInt(data.get("SEPT").toString()));
				monthY.add(Integer.parseInt(data.get("OCT").toString()));
				monthY.add(Integer.parseInt(data.get("NOV").toString()));
				monthY.add(Integer.parseInt(data.get("DEC").toString()));
				Map<String, Object> allYear = new HashMap<String, Object>();
				allYear.put("dateX", dateX);
				allYear.put("monthY", monthY);
				return allYear;
			} else {
				return null;
			}
		} else {
			Map<String, Object> error=new HashMap<String, Object>();
			return error;
		}

		/*
		 * List<Map<String,Object>> a =new ArrayList<Map<String,Object>>();
		 * Map<String,Object> test1=new HashMap<String, Object>();
		 * test1.put("test1", 1); Map<String,Object> test2=new HashMap<String,
		 * Object>(); test2.put("test2", 1); a.add(test1); a.add(test2);
		 * JSONArray result=JSONArray.fromObject(a);
		 */
	}

	public List<String> getYearMonth() throws ParseException {
		List<String> months = new ArrayList<String>();
		Calendar c = Calendar.getInstance();
		int year = c.get(Calendar.YEAR);
		for (int i = 1; i < 13; i++) {
			String StringI = "";
			if (i < 10) {
				StringI = "0" + String.valueOf(i);
			} else {
				StringI = String.valueOf(i);
			}
			String example = String.valueOf(year) + "-" + StringI;
			months.add(example);
		}
		return months;
	}

}
